package org.apache.cassandra.cql.jdbc;

import java.net.URI;
import java.net.URISyntaxException;
import java.sql.SQLException;
import java.sql.SQLNonTransientConnectionException;
import java.sql.SQLSyntaxErrorException;
import java.util.Properties;

public  class LaUDCassandraUtils extends Utils {
	//change protected to public
	public static final String SCHEMA_MISMATCH=Utils.SCHEMA_MISMATCH;
	public static final String NO_RESULTSET=Utils.NO_RESULTSET;
	public static class UriProperties{
		public UriProperties(Properties props, URI uri2) {
			this.properties=props;
			this.uri=uri2;
		}
		public Properties properties;
		public URI uri;
	}
	public static  UriProperties parseURL(String url,Properties props ) throws SQLException
	{
		URI uri = null;
		if (!(url == null))
		{
			props.setProperty(TAG_PORT_NUMBER, "" + DEFAULT_PORT);
			String rawUri = url.substring(PROTOCOL.length());

			try
			{
				uri = new URI(rawUri);
			}
			catch (URISyntaxException e)
			{
				throw new SQLSyntaxErrorException(e);
			}

			String host = uri.getHost();
			if (host == null) throw new SQLNonTransientConnectionException(HOST_IN_URL);
			props.setProperty(TAG_SERVER_NAME, host);

			int port = uri.getPort() >= 0 ? uri.getPort() : DEFAULT_PORT;
			props.setProperty(TAG_PORT_NUMBER, "" + port);

			String keyspace = uri.getPath();
			if ((keyspace != null) && (!keyspace.isEmpty()))
			{
				if (keyspace.startsWith("/")) keyspace = keyspace.substring(1);
				if (!keyspace.matches("[a-zA-Z]\\w+"))
					throw new SQLNonTransientConnectionException(String.format(BAD_KEYSPACE, keyspace));
				props.setProperty(TAG_DATABASE_NAME, keyspace);
			}
			String query = uri.getQuery();
			if ((query != null) && (!query.isEmpty()))
			{
				String[] items = query.split("&");
				//  if (items.length != 1) throw new SQLNonTransientConnectionException(URI_IS_SIMPLE);
				for(String item:items){
					String[] option = item.split("=");
					props.put(option[0], option[1]);
				}
			}

//			if (uri.getUserInfo() != null)
//				throw new SQLNonTransientConnectionException(URI_IS_SIMPLE);
			if(uri.getUserInfo()!=null){
				String userInfo=uri.getUserInfo();
				int split=userInfo.indexOf(':');
				props.put(TAG_USER, userInfo.substring(0, split));
				props.put(TAG_PASSWORD, userInfo.substring(split+1));
			}
		}

		//if (logger.isTraceEnabled()) logger.trace("URL : '{}' parses to: {}", url, props);

		return new UriProperties(props,uri);
	}

	//	public  String main() throws InvalidRequestException{
	//		String testString="";
	//		ByteBuffer bb=compressQuery(testString, Compression.GZIP);
	//		ByteBuffer cc=ByteBufferUtil.EMPTY_BYTE_BUFFER;
	//		ByteBuffer dd=ByteBuffer.wrap("".getBytes());
	//		System.out.println( dd.equals(cc));
	//		String str=uncompress(cc, Compression.NONE);
	//		return str;
	//	}
	//	public static void main(String[]strs) throws InvalidRequestException{
	//		LaUDCassandraUtils utils=new LaUDCassandraUtils();
	//		String str=utils.main();
	//		System.out.println(str);
	//	}
}
